<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\RunErrand;
use App\Enums\RunErrandEnum;
use App\Models\BalanceRecord;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class RunErrandController extends AdminController
{
    protected $status = [1 => '待审核', 2 => '审核通过', 3 => '审核失败'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new RunErrand(['school', 'faculty', 'user']), function (Grid $grid) {
            if (Admin::user()->school_id) {
                $grid->model()->where('school_id', Admin::user()->school_id);
            }
            $grid->column('id')->sortable();
            $grid->column('nick_name');
            $grid->column('mobile');
            $grid->column('user.balance');
            $grid->column('总营收')->display(function () {
                $userID = $this->user->id;
                return $totalIncome = BalanceRecord::where('user_id', $userID)->where('type', 1)->sum('amount');
            });
            $grid->column('id_number');
            $grid->column('school.name');
            $grid->column('faculty.name');
            $grid->column('specialty');
            $grid->column('status')->using($this->status);
            $grid->column('id_card_front')->image('', 50);
            $grid->column('id_card_reverse_side')->image('', 50);
            $grid->column('blacklists')->using([1 => '否', 2 => '是'])->label([
                1 => 'success',
                2 => 'danger',
            ]);
            $grid->column('created_at');
            $grid->column('updated_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id')->width(2);
                $filter->like('nick_name')->width(3);
                $filter->like('mobile')->width(3);
            });
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new RunErrand(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('nick_name');
            $show->field('mobile');
            $show->field('id_number');
            $show->field('school_id');
            $show->field('specialty');
            $show->field('status');
            $show->field('id_card_front');
            $show->field('id_card_reverse_side');
            $show->field('fail_reason');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new RunErrand(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('nick_name');
            $form->text('mobile');
            $form->text('id_number');
            $form->text('school_id');
            $form->text('specialty');
            $form->radio('status')->options($this->status)->when(3, function (Form $form) {
                $form->text('fail_reason');
            });
            $form->image('id_card_front')->move('images/id_card')->uniqueName()->autoUpload()->removable(false);
            $form->image('id_card_reverse_side')->move('images/id_card')->uniqueName()->autoUpload()->removable(false);
            $form->radio('blacklists')->options([1 => '否', 2 => '是']);
        })->saving(function (Form $form) {
            $status = $form->input('status');
            $failReason = $form->input('fail_reason');
            if ($status == 3 && $failReason == '') {
                return $form->response()->error('请填写失败原因');
            }
        });
    }
}
